<?php


/**
 * 设置分身
 * auction.js setdaemon
 */
session_start();
define('ROOT_PATH', str_replace("\\", '/', dirname(__FILE__)));
$config = include(ROOT_PATH.'/Lib/common.ini.php');

	$data = Init_GP(array('id'));
	$id = (int)($data['id']);
	$userdata = $_SESSION['user'];
	$sql = "SELECT * FROM `{$prefix}user` where `id`={$userdata['id']}";
	$userdata = $db->fetch_first($sql);
	if ($userdata == false){
		$msg = 2;  //请先登录	
	}else {
		$auctiondata = $get->getAuctionData($id);
		
		if (empty($auctiondata)){
			$sql = "SELECT * FROM `{$prefix}auction` where `id`={$id}";
			$auctiontmp = $db->fetch_first($sql);
			if (empty($auctiontmp)) {
				$msg = 5; //竞拍不存在
			}else {
				if ($auctiontmp['state'] == 0){
					$msg = 8;  //竞拍未开始
				}elseif ($auctiontmp['state'] >= 2){
					$msg =9; //竞拍已结束
				}else{
					$msg = 10; //异常错误
				}
			}
		}else {
			if($auctiondata['type'] == 1){
				if($userdata['old'] == 1)$msg = 4;//新人竞拍
				if($userdata['state'] != 1)$msg = 6;//新人竞拍
			}else {
				$bool = true;
				if(!empty($auctiondata['boutlimit'])){
					$sql = "SELECT count(*) as count FROM `{$prefix}auction_log` where `aid` = {$auctiondata['id']} and `uid` = {$userdata['id']}";
					$bout = $db->fetch_first($sql);
					$bout = $bout['count'];
					if($bout >= $auctiondata['boutlimit']){
						$bool = false;
					}
				}
				if($bool){		
					if ($userdata['priority'] == 1){  //优先使用闪点
						if ($userdata['beat'] < $auctiondata['usebeat']){
							if ($userdata['pointb'] < $auctiondata['usebeat']){
								$msg = 3; //余额不足
							}else {
								//执行拍点竞拍操作
								$id=setdaemon($userdata,$auctiondata);
								if($id>0){
									$msg = 1;
								}elseif($id==0){
									$msg = 11;
								}elseif($id==-1) {
									$msg = 12;
								}else{
									$msg = 0;
								}
							}	
						}else {
							//执行闪点竞拍操作
							$id=setdaemon($userdata,$auctiondata);
							if($id>0){
								$msg = 1;
							}elseif($id==0){
								$msg = 11;
							}elseif($id==-1) {
								$msg = 12;
							}else{
							    $msg = 0;
							}
						}
					}elseif ($userdata['priority'] == 2){ //优先使用拍点
						if ($userdata['pointb'] < $auctiondata['usebeat']){
							if ($userdata['beat'] < $auctiondata['usebeat']){
								$msg = 3; //余额不足
							}else {
								//执行闪点竞拍操作
								$id=setdaemon($userdata,$auctiondata);
								if($id>0){
									$msg = 1;
								}elseif($id==0){
									$msg = 11;
								}elseif($id==-1) {
									$msg = 12;
								}else{
									$msg = 0;
								}
							}	
						}else {
							//执行拍点竞拍操作
							$id=setdaemon($userdata,$auctiondata);
							if($id>0){
								$msg = 1;
							}elseif($id==0){
								$msg = 11;
							}elseif($id==-1) {
								$msg = 12;
							}else{
							    $msg = 0;
							}
						}
					}

				}else{
					$msg = 7;
				}
			}
		}
	}
	
	echo $msg;	
	
	//设置守秒
	function setdaemon($userdata,$auctiondata){
		//编辑用户点数
		global $db,$prefix;
		$time = time();
		$snum=$auctiondata['endtime']-$time;
		if($auctiondata['iskeep']==1){
			if($snum>3){
				$sql = "SELECT * FROM `{$prefix}daemon` WHERE `aid` = {$auctiondata['id']} and `uid` = {$userdata['id']} and `type` = 0 and `state` = 0";
				$tmp = $db->fetch_first($sql);//读出来
				unset($sql);
				if(empty($tmp)){
					
					$sql = "INSERT INTO `{$prefix}daemon` (`id`, `aid`, `uid`, `starttime`, `startprice`, `endprice`, `num`, `addtime`, `type`, `state`) 
							VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '0', '0', '0', '1', '{$time}', '0', '0')";
					$db->query($sql);// 写进数据库
					$id = $db->insert_id(); //获取刚才写入的ID
					unset($sql);
					return $id;	
				}else return 0;
			}else return -1;
		}else return -10;
	}
?>